package com.itheima.reggie.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.security.auth.callback.LanguageCallback;
import java.util.List;
@Slf4j
@Service
@Transactional
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;


    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    // 分页+条件查询
    public Page<Dish> findByPage(Integer pageNum, Integer pageSize, String name) {
        //构建菜品条件
        LambdaQueryWrapper<Dish> qw=new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotBlank(name),Dish::getName,name);
        //开启分页
        Page<Dish> page = new Page<>(pageNum, pageSize);
        // 3.分页查询
        page=dishMapper.selectPage(page,qw);
        // 4.遍历菜品列表
        List<Dish> dishList=page.getRecords();
        for (Dish dish : dishList) {
            // 根据菜品id查询口味列表
            LambdaQueryWrapper<DishFlavor> dqw=new LambdaQueryWrapper<>();
            dqw.eq(DishFlavor::getId,dish.getId());
            List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(dqw);
            // 封装口味列表
            dish.setFlavors(dishFlavorList);
            // 根据分类id查询分类信息
            Category category=categoryMapper.selectById(dish.getCategoryId());
            // 封装分类名称
            dish.setCategoryName(category.getName());
        }


        return page;
    }

    @Override
    public void save(Dish dish) {
        //1新增菜品基本信息
        log.info("新增之前"+dish.getId());
        dishMapper.insert(dish);
        log.info("新增之后"+dish.getId());//主键返回
        //遍历口味
        for (DishFlavor flavor:dish.getFlavors()){
            //关联菜品id
            flavor.setDishId(dish.getId());
            //新增口味
            dishFlavorMapper.insert(flavor);
        }

    }

    @Override
        public void updateById(Dish dish) {
            //1修改菜品基本信息
            dishMapper.updateById(dish);
            //根据菜品id删除原来口味
            LambdaQueryWrapper<DishFlavor> qw=new LambdaQueryWrapper<>();
            qw.eq(DishFlavor::getDishId,dish.getId());
            dishFlavorMapper.delete(qw);
            //遍历新增口味
            for (DishFlavor flavor : dish.getFlavors()) {
                // 关联菜品id
                flavor.setDishId(dish.getId());
                // 口味新增
                dishFlavorMapper.insert(flavor);
            }


    }
    // 回显菜品
    @Override
    public Dish findById(long id) {
        //先查菜品
        Dish dish = dishMapper.selectById(id);
        // 2.根据菜品id查询口味
        // 2-1 构建条件
        LambdaQueryWrapper<DishFlavor>qw=new LambdaQueryWrapper<>();
        qw.eq(DishFlavor::getDishId,id);
        // 2-2  调用mapper
        List<DishFlavor>dishFlavorList=dishFlavorMapper.selectList(qw);
        //3.将口味设置到菜品
        dish.setFlavors(dishFlavorList);
        //  4.返回菜品
        return dish;
    }

    @Override
    public void deleteBatchIds(List<Long> ids) {
        //查询菜品是否为停售
        LambdaQueryWrapper<Dish> qw=new LambdaQueryWrapper<>();
        qw.in(Dish::getId,ids);//in 代表多个值 1 3 5
        qw.eq(Dish::getStatus,1);//and status=1停售
        Integer count=dishMapper.selectCount(qw);
        if (count>0){
            throw new CustomException("删除菜品状态必须为停售");
        }
        //调用mapper删除菜品
        dishMapper.deleteBatchIds(ids);
        //删除口味
        LambdaQueryWrapper<DishFlavor> wrapper=new LambdaQueryWrapper<>();

        wrapper.in(DishFlavor::getDishId,ids);
        dishFlavorMapper.delete(wrapper);

    }

    @Override
    public void updateStatus(Integer status, List<Long> ids) {
        //条件对象
        LambdaQueryWrapper<Dish> wrapper=new LambdaQueryWrapper<>();
        wrapper.in(Dish::getId,ids);
        //修改实体
        Dish dish=new Dish();
        dish.setStatus(status);
        //调用mapper
        dishMapper.update(dish,wrapper);
    }
    // 根据分类查询菜品列表
    @Override
    public List<Dish> findListByCategoryId(Long categoryId,String name) {
        LambdaQueryWrapper<Dish>qw=new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotBlank(name),Dish::getName,name);
        qw.eq(categoryId!=null ,Dish::getCategoryId,categoryId);//根据分类查询
        qw.eq(Dish::getStatus,1);//起售状态
        return dishMapper.selectList(qw);
    }
    // 菜品展示
    @Override
    public List<Dish> findList(Long categoryId, Integer status) {
        // 1.查询该分类下起售的菜品有哪些
        // 1-1构建条件
        LambdaQueryWrapper<Dish> qw=new LambdaQueryWrapper<>();
        qw.eq(Dish::getCategoryId,categoryId);
        qw.eq(Dish::getStatus,status);
        // 1-2mapper查询
        List<Dish> dishList = dishMapper.selectList(qw);
        // 2.遍历菜品列表
        for (Dish dish : dishList) {
            // 根据菜品id查询口味
            List<DishFlavor> dishFlavorList = dishFlavorMapper.selectList(new LambdaQueryWrapper<DishFlavor>().eq(DishFlavor::getDishId, dish.getId()));
            //添加到菜品中
            dish.setFlavors(dishFlavorList);
        }  // 3.返回结果
        return dishList;
    }
}
